package com.zzq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzq.domain.entity.SmilePhotographerDividendRule;
import com.zzq.enumerate.DividendTypeEnum;
import com.zzq.exception.NormalException;
import com.zzq.mapper.SmilePhotographerDividendRuleMapper;
import com.zzq.service.SmilePhotographerDividendRuleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzq.util.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 小布
 * @since 2023-10-08
 */
@Service
public class SmilePhotographerDividendRuleServiceImpl extends ServiceImpl<SmilePhotographerDividendRuleMapper, SmilePhotographerDividendRule> implements SmilePhotographerDividendRuleService {

    @Autowired
    SmilePhotographerDividendRuleMapper photographerDividendRuleMapper;
    @Override
    public SmilePhotographerDividendRule getShootRule(BigDecimal amount) {
        LambdaQueryWrapper<SmilePhotographerDividendRule> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(SmilePhotographerDividendRule::getPhotographerDividendType, DividendTypeEnum.SHOOT_DIVIDEND.getName());
        List<SmilePhotographerDividendRule> smilePhotographerDividendRules = photographerDividendRuleMapper.selectList(queryWrapper);
        if (!ObjectUtils.isEmpty(smilePhotographerDividendRules)){
        /**
         * 获取集合通过过滤获取合适对象
         */
        Optional<SmilePhotographerDividendRule> DividendRuleOptional = smilePhotographerDividendRules.stream().filter(rule ->
                amount.compareTo(rule.getPhotographerDividendMinimumAmount()) > -1
                && amount.compareTo(rule.getPhotographerDividendSatisfiedAmount()) == -1

        ).findFirst();
        SmilePhotographerDividendRule smilePhotographerDividendRule = DividendRuleOptional.get();
        return smilePhotographerDividendRule;
        }else {
            throw new NormalException("分红规则数据缺失");
        }
    }

    @Override
    public SmilePhotographerDividendRule getCommissionRule() {
        LambdaQueryWrapper<SmilePhotographerDividendRule> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(SmilePhotographerDividendRule::getPhotographerDividendType, DividendTypeEnum.COMMISSION_DIVIDEND.getName());
        List<SmilePhotographerDividendRule> smilePhotographerDividendRules = photographerDividendRuleMapper.selectList(queryWrapper);
        if (ObjectUtils.isListSole(smilePhotographerDividendRules)) {
            return smilePhotographerDividendRules.stream().findFirst().get();
        }
        return null;
    }
}
